package com.amumu.drama.common.modules.service.impl.fulu;

import cn.hutool.json.JSONUtil;
import com.amumu.drama.common.constant.SkuOnTypeEnum;
import com.amumu.drama.common.modules.fulu.dto.ProductModifyDto;
import com.amumu.drama.common.modules.service.FuluCallbackService;
import com.amumu.drama.common.utils.MD5Utils;
import com.amumu.drama.common.utils.MagicNumConstant;
import com.amumu.drama.mapper.PmsSkuStockMapper;
import com.amumu.drama.model.PmsSkuStock;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @Auther: DM
 * @Date: 2024/5/15 10:13
 * @Description:微丸子-福禄回调接口
 */
@Service
public class FuluCallbackServiceImpl implements FuluCallbackService {
    @Value("${remote-service.ai-agent.sysSecret:55e7283678e54939b9830362fc5423fb}")
    private String sysSecret;
    @Autowired
    private PmsSkuStockMapper skuStockMapper;

    @Override
    public int skuProductCallBack(ProductModifyDto productModifyDto) {
        // 根据传入的数据进行验签
        if(!Objects.equals(productModifyDto.getSign(),sign(productModifyDto,sysSecret))){
            throw new RuntimeException("验签失败,有人篡改信息");
        }
        QueryWrapper<PmsSkuStock> productTemplateQueryWrapper = new QueryWrapper<>();
        productTemplateQueryWrapper.eq("sku_code", productModifyDto.getProduct_id());
        PmsSkuStock pmsSkuStock = skuStockMapper.selectOne(productTemplateQueryWrapper);
        if (Objects.isNull(pmsSkuStock)){
           throw new RuntimeException("当前商品sku不存在，请及时同步");
        }
        //  根据商品变更类型:修改价格或者商品状态
        if (Objects.equals(productModifyDto.getChanged_type(), MagicNumConstant.ONE)){
            pmsSkuStock.setDirectPublishStatus(SkuOnTypeEnum.getByDesc(productModifyDto.getProduct_sale_status()).getCode());
        } else if(Objects.equals(productModifyDto.getChanged_type(), MagicNumConstant.TWO)) {
            pmsSkuStock.setPrice(new BigDecimal(productModifyDto.getProduct_price()).multiply(new BigDecimal(MagicNumConstant.ONE_HUNDRED)));
        }
        return skuStockMapper.updateById(pmsSkuStock);

    }

    public static String sign(ProductModifyDto productModifyDto, String sysSecret) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("product_id", productModifyDto.getProduct_id());
        map.put("changed_type", productModifyDto.getChanged_type());
        map.put("product_sale_status", productModifyDto.getProduct_sale_status());
        map.put("product_price", productModifyDto.getProduct_price());

        // 将Map转换为JSON字符串
        String jsonStr = JSONUtil.toJsonStr(map);
        // 将JSON字符串转换为JSONObject
        char[] s = jsonStr.toCharArray();
        Arrays.sort(s);
        String outputSignOriginalStr = new String(s) + sysSecret;
        String sign = MD5Utils.MD5(outputSignOriginalStr);
        return sign;
    }

}
